package com.libraries.base;

import com.example.libraries.R;
import com.libraries.helper.DeviceHelper;
import com.libraries.helper.Logger;
import com.libraries.view.listener.ILoadDialogListener;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

/**
 * Activity基类 定义统一回调接口 生命周期监听
 *
 */
@SuppressLint({ "NewApi", "ResourceAsColor" })
public class BaseActivity extends Activity implements ILoadDialogListener {
	private boolean visible;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		BaseApplication.registerListener(this);
		DeviceHelper.setStatusBarColor(this, R.color.status_bar);
		visible = true;
	}

	public boolean isVisible() {
		return visible;
	}

	@Override
	protected void onResume() {
		super.onResume();
		Logger.log(this, "onResume");
//		MobclickAgent.onPageStart(this.getClass().getSimpleName());
//		MobclickAgent.onResume(this); // 统计时长
		visible = true;
	}

	@Override
	protected void onPause() {
		super.onPause();
		visible = false;
		Logger.log(this, "onPause");
//		MobclickAgent.onPageEnd(this.getClass().getSimpleName());
//		MobclickAgent.onPause(this); // 统计时长
		// Constants.onHomed=DeviceHelper.onHome(this);
	}

	@Override
	protected void onStop() {
		super.onStop();
		visible = false;
		Logger.log(this, "onStop");
	}

	@Override
	public void dismiss(boolean interrupt) {
		Logger.log(this, "dismiss", interrupt);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			doActivityResult(RESULT_CANCELED);
		}
		return super.onKeyDown(keyCode, event);
	}

	public void doActivityResult(int resultCode) {
		setResult(resultCode);
		finish();
	}

	public void doActivityResult(int resultCode, Intent intent) {
		setResult(resultCode, intent);
		finish();
	}

	/**
	 * 界面已经不再可见的时候，我们应当将所有和界面相关的资源进行释放。在这种场景下释放资源可以让系统缓存后台进程的能力显著增加，
	 * 因此也会让用户体验变得更好。
	 */
	@Override
	public void onTrimMemory(int level) {
		super.onTrimMemory(level);
		Logger.log(this, "onTrimMemory", level);
		switch (level) {
		case TRIM_MEMORY_UI_HIDDEN:
			// 进行资源释放操作
			break;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Logger.log(this, "onDestroy");
//		BaseApplication.unregisterListener(this);
	}

}